package com.almadenresearch.vtawifilogin;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

public class WifiReceiver extends BroadcastReceiver {
	public WifiReceiver() {
	}

	@Override
	public void onReceive(Context context, Intent intent) {
        ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
        NetworkInfo netInfo = conMan.getActiveNetworkInfo();
        if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            Log.d("WifiReceiver", "Have Wifi Connection");
            // Do set up
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            Log.d("wifiInfo", wifiInfo.toString());
            Log.d("SSID",wifiInfo.getSSID());
            // Get SSID
            String currentSSID = wifiInfo.getSSID();
            // Get SSID from Property
            String correctSSID = "VTA Free Wifi"; // pull from preferences
            
            // if SSID, and SSIDfromProperty are ==, then do web stuff.
            if(currentSSID.equals(correctSSID)) {
            	Log.d("WifiReceiver","found right SSID");
            	// now pull up the web page, check the box and push submit
            	// then push a notification
            	SubmitPage sp = new SubmitPage();
            	if(sp.doPage()) 
            		Log.d("WifiReceiver","Successful page submission");
            	else
            		Log.d("WifiReceiver","Failed page submission");
            }
        }
        else
            Log.d("WifiReceiver", "Don't have Wifi Connection");    
	}
}
